﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;

namespace ModuleDuoc.DM
{
    public partial class DM_NhomThuoc : Form
    {
        public DM_NhomThuoc()
        {
            InitializeComponent();
        }

        private void DM_NhomThuoc_Load(object sender, EventArgs e)
        {
            this.dgvData.AutoGenerateColumns = false;
            this.fillNhom();
        }

        private void fillNhom()
        {
            string sql = "SELECT * FROM zD_DM_VATTU_NHOMVATTU zdvn ORDER BY zdvn.ID";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            this.dgvData.DataSource = dt;        
        }

        private void dgvData_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.txtTenNhom.Enabled = false;
            this.txtGhiChu.Enabled = false;
            this.txtMoTa.Enabled = false;

            string sql = "SELECT * FROM zD_DM_VATTU_NHOMVATTU zdvn WHERE zdvn.MANHOM = '"+this.dgvData.Rows[this.dgvData.CurrentRow.Index].Cells["MaNhom"].Value.ToString()+"'";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            this.txtMaNhom.Text = dt.Rows[0]["MaNhom"].ToString();
            this.txtTenNhom.Text = dt.Rows[0]["TenNhom"].ToString();
            this.txtGhiChu.Text = dt.Rows[0]["GhiChu"].ToString();
            this.txtMoTa.Text = dt.Rows[0]["MoTa"].ToString();
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            this.view_clear();
        }

        private void view_clear()
        {
            this.txtMaNhom.Text = "";
            this.txtTenNhom.Enabled = true;
            this.txtGhiChu.Enabled = true;
            this.txtMoTa.Enabled = true;
            this.txtTenNhom.Text = "";
            this.txtGhiChu.Text = "";
            this.txtMoTa.Text = "";
            this.txtTenNhom.Focus();
        }

        private bool checkInput()
        {
            if (this.txtTenNhom.Text == "" || this.txtTenNhom.Text.Length > 30)
            {
                MessageBox.Show("Dữ liệu nhập không hợp lệ (Không để trống và <= 30 kí tự!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtTenNhom.Focus();
                return false;
            }
            else if (this.txtGhiChu.Text.Length > 100)
            {
                MessageBox.Show("Dữ liệu nhập không hợp lệ (<= 100 kí tự!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtGhiChu.Focus();
                return false;
            }
            else if (this.txtMoTa.Text.Length > 50)
            {
                MessageBox.Show("Dữ liệu nhập không hợp lệ (<= 50 kí tự!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtMoTa.Focus();
                return false;
            }
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtTenNhom.Enabled == true & checkInput() == true)
            {
                if (this.txtMaNhom.Text != "")
                {
                    //Update
                    BE.ZD_DM_VATTU_NHOMVATTU be = new BE.ZD_DM_VATTU_NHOMVATTU();
                    BC.ZD_DM_VATTU_NHOMVATTUBC bc = new BC.ZD_DM_VATTU_NHOMVATTUBC();

                    be.MANHOM = this.txtMaNhom.Text;
                    be.TENNHOM = txtTenNhom.Text;
                    be.GHICHU = txtGhiChu.Text;
                    be.MOTA = txtMoTa.Text;

                    bc.Update(be);
                    MessageBox.Show("Cập nhật thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);                
                }
                else
                { 
                    //Add
                    BE.ZD_DM_VATTU_NHOMVATTU be = new BE.ZD_DM_VATTU_NHOMVATTU();
                    BC.ZD_DM_VATTU_NHOMVATTUBC bc = new BC.ZD_DM_VATTU_NHOMVATTUBC();

                    be.MANHOM = getMaxMaNhom();
                    be.TENNHOM = txtTenNhom.Text;
                    be.GHICHU = txtGhiChu.Text;
                    be.MOTA = txtMoTa.Text;

                    bc.Insert(be);
                    MessageBox.Show("Thêm mới thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                this.fillNhom();
                view_clear();
                this.txtTenNhom.Focus();
            }
        }

        private string getMaxMaNhom()
        {
            string sql = "SELECT MAX(CONVERT(INT, zdvn.MANHOM + 1)) FROM zD_DM_VATTU_NHOMVATTU zdvn";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            return dt.Rows[0][0].ToString();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            this.txtTenNhom.Enabled = true;
            this.txtGhiChu.Enabled = true;
            this.txtMoTa.Enabled = true;
        }
    }
}
